package com.ss.android.common.app;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

public abstract class AbsPagerAdapter extends PagerAdapter {
    public final FragmentManager mFragmentManager;
    public FragmentTransaction mFragmentTransaction = null;
    public Fragment mFragment = null;

    public AbsPagerAdapter(FragmentManager fragmentManager) {
        this.mFragmentManager = fragmentManager;
    }

    public final Object instantiateItem(ViewGroup container, int position) {
        if (this.mFragmentTransaction == null) {
            this.mFragmentTransaction = this.mFragmentManager.beginTransaction();
        }
        this.c(position);
        String tag = this.getTag(container.getId(), position);
        Fragment fragment = this.mFragmentManager.findFragmentByTag(tag);
        if (fragment != null) {
            this.mFragmentTransaction.attach(fragment);
        } else {
            fragment = this.createFragment(position);
            this.mFragmentTransaction.add(container.getId(), fragment, tag);
        }

        if (fragment != this.mFragment) {
            fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }

        return fragment;
    }

    public final String getTag(int arg3, int arg4) {
        return "android:switcher:" + arg3 + ":" + this.d(arg4);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (this.mFragmentTransaction == null) {
            this.mFragmentTransaction = this.mFragmentManager.beginTransaction();
        }
        this.mFragmentTransaction.detach(((Fragment) object));
    }

    @Override
    public final boolean isViewFromObject(View view, Object object) {
        return ((Fragment) object).getView() == view;
    }

    public abstract Fragment createFragment(int position);

    @Override
    public final void finishUpdate(View container) {
        if (this.mFragmentTransaction != null) {
            try {
                this.mFragmentTransaction.commitAllowingStateLoss();
                this.mFragmentTransaction = null;
                this.mFragmentManager.executePendingTransactions();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    }

    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        if (object != this.mFragment) {
            if (this.mFragment != null) {
                this.mFragment.setMenuVisibility(false);
                this.mFragment.setUserVisibleHint(false);
            }
            if (object != null) {
                ((Fragment) object).setMenuVisibility(true);
                ((Fragment) object).setUserVisibleHint(true);
            }
            this.mFragment = ((Fragment) object);
        }
    }

    public long c(int arg3) {
        return ((long) arg3);
    }

    public String d(int arg3) {
        return String.valueOf(this.c(arg3));
    }
}

